﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Auditors_EnterPurchase2 : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string str1 = "<script>var x = ['',";

            foreach (int v in Enum.GetValues(typeof(CurrencyEnum)))
            {
                CurrencyEnum pStatus = (CurrencyEnum)v;
                dropCurrency.Items.Add(new ListItem(RemarkAttribute.GetEnumRemark(pStatus), v.ToString()));
                str1 += "'" + RemarkAttribute.GetEnumRemark(pStatus) + "',";

                if (v == 5)
                {
                    str1 += "'',";
                }
            }

            str1 = str1.TrimEnd(',') + "]</script>";
            Label1.Text = str1;


            string company = Request.QueryString["CompanyID"];
            string orderid = Request.QueryString["orderid"];
            var AuditorsEntity = new CompanyBLL().GetCompanyAuditorsEntityByOrderId(orderid);
            if (AuditorsEntity != null)
            {

                List<ContrastAmount> banks = new AuditorsBLL().GetBankSalesAmount(company, orderid, ((int)ExplainEnum.Purchase).ToString());
                List<ContrastAmount> purchase = new AuditorsBLL().GetPurchaseAmount(company, orderid);

                StringBuilder sb = new StringBuilder();
                sb.Append("<table>");
                sb.Append("<tr>");
                sb.AppendFormat("<th>{0}</th>", "日期");
                sb.AppendFormat("<th>{0}</th>", "银行流水");
                sb.AppendFormat("<th>{0}</th>", "发票金额");
                sb.AppendFormat("<th>{0}</th>", "差额");
                sb.Append("</tr>");

                for (DateTime dt = AuditorsEntity.StartTime; dt <= AuditorsEntity.EndTime; dt = dt.AddMonths(1))
                {
                    double d = 0;
                    sb.Append("<tr>");
                    sb.AppendFormat("<th>{0}</th>", dt.ToString("yyyy年MM月"));
                    if (banks.Count(fun => fun.Date.Year == dt.Year && fun.Date.Month == dt.Month) > 0)
                    {
                        d = banks.First(fun => fun.Date.Year == dt.Year && fun.Date.Month == dt.Month).Amont;
                        sb.AppendFormat("<th>{0}</th>", d);
                    }
                    else
                    {
                        sb.AppendFormat("<th>{0}</th>", 0);
                    }

                    if (purchase.Count(fun => fun.Date.Year == dt.Year && fun.Date.Month == dt.Month) > 0)
                    {
                        d = d - purchase.First(fun => fun.Date.Year == dt.Year && fun.Date.Month == dt.Month).Amont;
                        sb.AppendFormat("<th>{0}</th>", purchase.First(fun => fun.Date.Year == dt.Year && fun.Date.Month == dt.Month).Amont);
                    }
                    else
                    {
                        sb.AppendFormat("<th>{0}</th>", 0);
                    }

                    sb.AppendFormat("<th>{0}</th>", d);
                    sb.Append("</tr>");
                }

                sb.Append("</table>");

                Label2.Text = sb.ToString();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Purchase"];

        if (cookie != null)
        {
            List<CreatePurchaseEntity> list = new List<CreatePurchaseEntity>();
            string company = Request.QueryString["CompanyID"];
            string orderid = Request.QueryString["orderid"];
            foreach (string s in System.Web.HttpUtility.UrlDecode(cookie.Value).TrimEnd(';').Split(';'))
            {
                list.Add(new CreatePurchaseEntity()
                {
                    VoucherNo = s.Split('~')[0],
                    Date = s.Split('~')[1],
                    PoNo = s.Split('~')[2],
                    Supplier = s.Split('~')[3],
                    Offering = s.Split('~')[4],
                    Amount = s.Split('~')[5],
                    Currency = dropCurrency.SelectedValue,
                    OrderId = orderid,
                    CompanyID = company
                });

            }
            string msg;

            if (new AuditorsBLL().CreatePurchase(list, out msg))
            {
                Response.Redirect("SelectPurchase.aspx?CompanyID=" + company + "&orderid=" + orderid);
            }
            else
            {
                if (msg.ToLower().StartsWith("sql自定义异常:"))
                {
                    AlertMsg(msg.ToLower().Replace("sql自定义异常:", ""));
                }
                else
                {
                    AlertMsg("插入失败，请检查数据是否异常");
                }
            }
        }

    }
}